unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Afficher, melanger, ProcedureUnit, verifier, Menus, Bot;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Joueur1: TLabel;
    Joueur2: TLabel;
    Joueur3: TLabel;
    Joueur4: TLabel;
    label5: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    LettresJoueur1: TLabel;
    LettresJoueur2: TLabel;
    LettresJoueur3: TLabel;
    LettresJoueur4: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    ScoreJoueur1: TLabel;
    ScoreJoueur2: TLabel;
    ScoreJoueur4: TLabel;
    ScoreJoueur3: TLabel;
    MainMenu1: TMainMenu;
    Ficher: TMenuItem;
    Edition1: TMenuItem;
    Annuler1: TMenuItem;
    Label10: TLabel;
    insert: TLabel;
    LblTour: TLabel;
    LblRestantes: TLabel;
    Joueur1Echanger: TButton;
    Joueur2Echanger: TButton;
    Joueur3Echanger: TButton;
    Joueur4Echanger: TButton;
    Button2: TButton;
    EnCour: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Annuler1Click(Sender: TObject);
    procedure Joueur1EchangerClick(Sender: TObject);
    procedure Joueur3EchangerClick(Sender: TObject);
    procedure Joueur2EchangerClick(Sender: TObject);
    procedure Joueur4EchangerClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);


  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;


var
  Form1: TForm1;
  DebutX, DebutY, NombreJoueur, CurJoueur, SacPos : Byte;
  PlayerName : array of String;
  Sac : Melanger.Tablo;
  Tour : Integer;
  PremLettre, Stop : Boolean;

implementation

{$R *.dfm}



Procedure BotJouer(Lettres : String);
var I , J, K, Z, M : Integer;
    ValeurRef, ValeurCur : Integer;
    MotRef : String;
    Hor, Meme : Boolean;
    TabloMot : Bot.tablo;
Begin

  ValeurRef := 0;
  M := 0;

 TabloMot := RechercherMotsEssai(Lettres);
    
 For I := 0 To High(TabloMot) Do
    Begin
      For J := 1 To 15 Do
        Begin
          Application.ProcessMessages;
          For K := 1 To 15 Do
            Begin
              IF ToutVerifier(J,k,TabloMot[I],Lettres,True) Then
                Begin
                  ValeurCur := ValeurMotFSimul(TabloMot[I],True,J, K, Length(TabloMot[I]));
                  IF ValeurCur > ValeurRef Then
                    Begin
                      ValeurRef := ValeurCur;
                      MotRef := TabloMot[I];
                      DebutX := J;
                      DebutY := K;
                      Hor := True;
                    End;
                End;
              IF ToutVerifier(K,J,TabloMot[I],Lettres,False) Then
                Begin
                  ValeurCur := ValeurMotFSimul(TabloMot[I],False,K, J, Length(TabloMot[I]));
                  IF ValeurCur > ValeurRef Then
                    Begin
                      ValeurRef := ValeurCur;
                      MotRef := TabloMot[I];
                      DebutX := K;
                      DebutY := J;
                      Hor := False;
                    End;
                End;
            End;
        End;
    End;
  IF MotRef = '' Then
    Begin
      {ShowMessage('Veuillez échanger toutes les lettres de l''ordinateur');}
      Stop := True;
      Exit;
    End;
  Form1.Edit1.Text := MotRef;
  IF Hor Then Form1.Button1Click(Form1.Label1) Else Form1.Button1Click(Form1.Label2);


End;



Procedure EchangerBtn;

Begin
  Case CurJoueur Of
  1 :
     Begin
       IF NombreJoueur = 2 Then
         Begin
           Form1.Joueur1Echanger.Enabled := True;
           Form1.Joueur2Echanger.Enabled := False;
         End;
       IF NombreJoueur = 3 Then
         Begin
           Form1.Joueur1Echanger.Enabled := True;
           Form1.Joueur3Echanger.Enabled := False;
         End;
       IF NombreJoueur = 3 Then
         Begin
           Form1.Joueur1Echanger.Enabled := True;
           Form1.Joueur4Echanger.Enabled := False;
         End;
     End;
  2 :
     Begin
       Form1.Joueur2Echanger.Enabled := True;
       Form1.Joueur1Echanger.Enabled := False;
     End;
  3 :
     Begin
       Form1.Joueur3Echanger.Enabled := True;
       Form1.Joueur2Echanger.Enabled := False;
     End;
  4 :
     Begin
       Form1.Joueur4Echanger.Enabled := True;
       Form1.Joueur3Echanger.Enabled := False;
     End;
  End;
End;


procedure TForm1.FormCreate(Sender: TObject);
var I : Integer;
    J: byte;
    ValeurCur, ValeurRef : byte;
    MotRef : String;
    Hor : Boolean;
begin
  Form1.DoubleBuffered := True;
  SacPos := 0;
  CurJoueur := 1;
  Sac := MelangerF;
  Insert.Caption := '0, 0';
  Tour := 1;
  PremLettre := True;
  OuvrirWords;
  Stop := False;

Repeat
  NombreJoueur := StrToInt(InputBox('Nombre de Joueur :','Combien y a-t''il de joueurs? ( 2 à 4)','2'));
Until (NombreJoueur > 1) And (NombreJoueur < 5);
  SetLength(PlayerName,NombreJoueur);
  For I := 0 to NombreJoueur - 1 Do
    PlayerName[I] := InputBox('Joueur' + IntToStr(I + 1), 'Entrez le nom du Joueur' + IntToStr(I + 1),'Joueur1');
  DebutX := 0;
  DebutY := 0;
  AfficherCarre(Image1);
  Afficher.Tab := ValeurCarreF;

  Joueur1.Caption := PlayerName[0];
  LettresJoueur1.Caption := '';
  For I := SacPos To SacPos + 6 Do
    Begin
      LettresJoueur1.Caption := LettresJoueur1.Caption + Chr(Sac[I] + 64);
    End;
  SacPos:= SacPos + 7;

  Joueur2.Caption := PlayerName[1];
  LettresJoueur2.Caption := '';
  For I := SacPos To SacPos + 6 Do
    Begin
      LettresJoueur2.Caption := LettresJoueur2.Caption + Chr(Sac[I] + 64);
    End;
  SacPos := SacPos + 7;

  IF NombreJoueur >= 3 Then
    Begin
      Joueur3Echanger.Visible := True;
      Joueur3.Visible := true;
      Label4.Visible := true;
      LettresJoueur3.Visible := true;
      Label9.Visible := true;
      ScoreJoueur3.Visible := true;
      Joueur3.Caption := PlayerName[2];
      LettresJoueur3.Caption := '';
      For I := SacPos To SacPos + 6 Do
        Begin
          LettresJoueur3.Caption := LettresJoueur3.Caption + Chr(Sac[I] + 64);
        End;
      SacPos := SacPos + 7;
    End;

  IF NombreJoueur = 4 Then
    Begin
      Joueur4Echanger.Visible := True;
      Joueur4.Visible := true;
      Label2.Visible := true;
      LettresJoueur4.Visible := true;
      Label8.Visible := true;
      ScoreJoueur4.Visible := true;
      Joueur4.Caption := PlayerName[3];
      LettresJoueur4.Caption := '';
      For I := SacPos To SacPos + 6 Do
        Begin
          LettresJoueur4.Caption := LettresJoueur4.Caption + Chr(Sac[I] + 64);
        End;
      SacPos := SacPos + 7;
    End;

  LblRestantes.caption := 'Lettres restantes : ' + IntToStr(100 - SacPos);

 {Ceci permet de faire jouer a l'ordinateur le premier coup.

 //----------------------------------------------------------------------------------
  Bot.MotBot := RechercherMots(LettresJoueur1.Caption);
  ValeurRef := 0;
  For I := 0 To High(Bot.MotBot) Do
    Begin
      For J := 8 - Length(Bot.MotBot[I]) + 1 To 8 DO
        Begin
          ValeurCur := ValeurMotFSimul(Bot.MotBot[I],True,J, 8, Length(Bot.MotBot[I]));
          IF Length(Bot.MotBot[I]) = 7 Then ValeurCur := ValeurCur + 50;
          IF ValeurCur > ValeurRef Then
            Begin
              ValeurRef := ValeurCur;
              MotRef := Bot.MotBot[I];
              Randomize;
              IF 1 = Random(2) + 1 Then
                Begin
                  DebutX := J;
                  DebutY := 8;
                  Hor := True;
                End
              Else
                Begin
                  DebutY := J;
                  DebutX := 8;
                  Hor := False;
                End;
            End;
        End;
    End;
    Edit1.Text := MotRef;
    Insert.Caption := IntToStr(DebutX) + ', ' + IntToStr(DebutY);
    IF Hor Then
      Form1.Button1Click(Label1)
    Else
      Form1.Button1Click(Label2);
//--------------------------------------------------------------------------------
}

End;




procedure TForm1.Button1Click(Sender: TObject);
var i, resultat, LettresATirees : byte;
    str, CurStr, LettresUtilisees, Lettres, StrUtilisees: string;
    Score: integer;
begin

  Image1.Canvas.Brush.Style := bsclear;
  Image1.Canvas.Font.style := [fsBold];
  Image1.Canvas.Font.Size := 25;
  Str := UpperCase(Edit1.Text);
  LettresUtilisees := '';


 Case CurJoueur Of
  1 : Begin
       Score := StrToInt(ScoreJoueur1.Caption);
       Lettres := LettresJoueur1.caption;
     End;
  2 : Begin
        Score := StrToInt(ScoreJoueur2.Caption);
        Lettres := LettresJoueur2.caption;
     End;
  3 : Begin
        Score := StrToInt(ScoreJoueur3.Caption);
        Lettres := LettresJoueur3.caption;
     End;
  4 : Begin
        Score := StrToInt(ScoreJoueur4.Caption);
        Lettres := LettresJoueur4.caption;
     End;
 End;

//--------------------------------------------------------------------------------
If Sender = Button1 Then
Begin

IF (debutX <> 0) And (DebutY <> 0) And(Str <> '') Then
  case (MessageDlg('Souhaitez-vous écrire le mot en Horizontal ou en vertical?? (Oui = Horizontal, Non = vertical)' + chr(13)+ 'Position : ' + IntToStr(DebutX) + ', ' + IntToStr(DebutY),mtInformation,mbYesNoCancel,0)) of
    MrYes :
      IF DebutX + Length(Str) <= 16 Then
        Begin

        IF Not VerifierNouveauMot(Length(Edit1.Text),DebutX,DebutY,True) Then
          Begin
            ShowMessage('Ces lettres sont déjà placées!');
            Exit;
          End;

        IF PremLettre Then
          IF Not (Verifier1erTour(Length(Edit1.Text), DebutX, DebutY, True)) Then
            Begin
              ShowMessage('Le premier mot que vous entrez dois passer par le centre!');
              Exit;
            End;



        If Not VerifierLettresF(UpperCase(Edit1.Text), UpperCase(Lettres), True, DebutX,DebutY) Then
          Begin
            Showmessage('Vous ne pouvez écrire ce mot avec les lettres en votre possession!');
            Exit;
          End;


        IF Not(PremLettre) Then
          IF (Not VerifierPos(Length(Edit1.Text),DebutX,DebutY,True)) Then
            Begin
              ShowMessage('Le mot que vous avez entrer ne coupe aucun mot');
              Exit;
            End;

            
          IF Not VerifierEntrer(Edit1.Text,True, DebutX, DebutY) Then Exit;

          Sauver(Image1, SacPos, CurJoueur, Score, Tour, Lettres, PremLettre);
          PremLettre := False;

          StrUtilisees := LettreUtilisees(Str, DebutX, DebutY, True);

          For I := 0 to Length(Trim(Str)) - 1 Do
            Begin
              Image1.Canvas.Textout(Image1.Width div 15 * (i + DebutX - 1) + 8, Image1.Height div 15 * (DebutY - 1) + 4, Str[I + 1]);
              IF Afficher.Plateau[DebutX + I,DebutY] = '' Then
                  LettresUtilisees := LettresUtilisees + Str[I + 1];
            End;
          Resultat := ValeurMotF(Str,True,DebutX,DebutY,Length(Str));
          For I := 0 To Length(Str) Do
            Afficher.Plateau[DebutX + I,DebutY] := Str[I+1];

           Str := StrUtilisees;
        End
      Else
        Begin
          ShowMessage('Le mot que vous avez entrez est trop long!');
          Exit;
        End;


    MrNo :


      IF DebutY + Length(Str) <= 16 Then
        Begin

        IF Not VerifierNouveauMot(Length(Edit1.Text),DebutX,DebutY,False) Then
          Begin
            ShowMessage('Ces lettres sont déjà placées!');
            Exit;
          End;

        IF PremLettre Then
          IF Not (Verifier1erTour(Length(Edit1.Text), DebutX, DebutY, False)) Then
            Begin
              ShowMessage('Le premier mot que vous entrez dois passer par le centre!');
              Exit;
            End;


        If Not VerifierLettresF(UpperCase(Edit1.Text), UpperCase(Lettres), False, DebutX,DebutY) Then
          Begin
            Showmessage('Vous ne pouvez écrire ce mot avec les lettres en votre possession!');
            Exit;
          End;


        IF Not(PremLettre) Then
          IF Not VerifierPos(Length(Edit1.Text),DebutX,DebutY,False) Then
            Begin
              ShowMessage('Le mot que vous avez entrer ne coupe aucun mot');
              Exit;
            End;


        IF Not VerifierEntrer(Edit1.Text,False, DebutX,  DebutY) Then Exit;

        Sauver(Image1, SacPos, CurJoueur, Score, Tour, Lettres, PremLettre);

        PremLettre := False;

        StrUtilisees := LettreUtilisees(str, DebutX, DebutY, False);

          For I := 0 To Length(Trim(Str)) - 1 do
            Begin
              Image1.Canvas.Textout(Image1.Width div 15 * (DebutX - 1) + 8, Image1.Height div 15 * (I + debutY - 1) + 4, Str[I + 1]);
              IF Afficher.Plateau[DebutX,DebutY + I] = '' Then
                  LettresUtilisees := LettresUtilisees + Str[I + 1];
            End;
          Resultat := ValeurMotF(Str,False,DebutX,DebutY,Length(Str));
          For I := 0 To Length(Str) Do
            Afficher.Plateau[DebutX,DebutY  + I] := Str[I+1];

          Str := StrUtilisees;
        End
      Else
        Begin
          ShowMessage('Le mot que vous avez entrez est trop long!');
          Exit;
        End;
    ELSE
      Exit;
    End
  Else
    Begin
      ShowMessage('Veulliez selectionnez un emplacement ou commencer ou entrer un mot a entrer');
      Exit;
    End;

End;

//---------------------------------------------------------------------------------

IF Sender = Label1 Then
  Begin
    IF DebutX + Length(Str) <= 16 Then
        Begin

        IF Not VerifierNouveauMot(Length(Edit1.Text),DebutX,DebutY,True) Then
          Begin
            ShowMessage('Ces lettres sont déjà placées!');
            Exit;
          End;

        IF PremLettre Then
          IF Not (Verifier1erTour(Length(Edit1.Text), DebutX, DebutY, True)) Then
            Begin
              ShowMessage('Le premier mot que vous entrez dois passer par le centre!');
              Exit;
            End;



        If Not VerifierLettresF(UpperCase(Edit1.Text), UpperCase(Lettres), True, DebutX,DebutY) Then
          Begin
            Showmessage('Vous ne pouvez écrire ce mot avec les lettres en votre possession!');
            Exit;
          End;


        IF Not(PremLettre) Then
          IF (Not VerifierPos(Length(Edit1.Text),DebutX,DebutY,True)) Then
            Begin
              ShowMessage('Le mot que vous avez entrer ne coupe aucun mot');
              Exit;
            End;

          IF Not VerifierEntrer(Edit1.Text,True, DebutX, DebutY) Then Exit;

          Sauver(Image1, SacPos, CurJoueur, Score, Tour, Lettres, PremLettre);
          PremLettre := False;

          StrUtilisees := LettreUtilisees(Str, DebutX, DebutY, True);

          For I := 0 to Length(Trim(Str)) - 1 Do
            Begin
              Image1.Canvas.Textout(Image1.Width div 15 * (i + DebutX - 1) + 8, Image1.Height div 15 * (DebutY - 1) + 4, Str[I + 1]);
              IF Afficher.Plateau[DebutX + I,DebutY] = '' Then
                  LettresUtilisees := LettresUtilisees + Str[I + 1];
            End;
          Resultat := ValeurMotF(Str,True,DebutX,DebutY,Length(Str));
          For I := 0 To Length(Str) Do
            Afficher.Plateau[DebutX + I,DebutY] := Str[I+1];

           Str := StrUtilisees;
        End
      Else
        Begin
          ShowMessage('Le mot que vous avez entrez est trop long!');
          Exit;
        End;
    End;

//-------------------------------------------------------------------------------------

If Sender = Label2 Then
  Begin
    IF DebutY + Length(Str) <= 16 Then
      Begin
       IF Not(PremLettre) Then
          IF Not VerifierPos(Length(Edit1.Text),DebutX,DebutY,False) Then
            Begin
              ShowMessage('Le mot que vous avez entrer ne coupe aucun mot');
              Exit;
            End;


        IF Not VerifierEntrer(Edit1.Text,False, DebutX, DebutY) Then Exit;

        Sauver(Image1, SacPos, CurJoueur, Score, Tour, Lettres, PremLettre);

        PremLettre := False;

        StrUtilisees := LettreUtilisees(str, DebutX, DebutY, False);

          For I := 0 To Length(Trim(Str)) - 1 do
            Begin
              Image1.Canvas.Textout(Image1.Width div 15 * (DebutX - 1) + 8, Image1.Height div 15 * (I + debutY - 1) + 4, Str[I + 1]);
              IF Afficher.Plateau[DebutX,DebutY + I] = '' Then
                  LettresUtilisees := LettresUtilisees + Str[I + 1];
            End;
          Resultat := ValeurMotF(Str,False,DebutX,DebutY,Length(Str));
          For I := 0 To Length(Str) Do
            Afficher.Plateau[DebutX,DebutY  + I] := Str[I+1];

          Str := StrUtilisees;
        End
      Else
        Begin
          ShowMessage('Le mot que vous avez entrez est trop long!');
          Exit;
        End;
  End;

//-----------------------------------------------------------------------------------------------



DebutX := 0;
DebutY := 0;
edit1.Text := '';

  Case CurJoueur Of
    1 :
      Begin
        CurStr := LettresJoueur1.caption;
        For I := 1 To Length(Str) Do
          Begin
            Delete(CurStr,Pos(Str[i], CurStr),1);
          End;

        Str := CurStr;

        Sac := Remelanger(Sac,SacPos);

        IF CurStr = '' Then ScoreJoueur1.Caption := IntToStr(StrToInt(ScoreJoueur1.Caption) + 50);

        LettresATirees := 6 - Length(CurStr);
        IF SacPos + LettresATirees > 99 Then
          LettresATirees := 99 - SacPos;

        For I := SacPos To SacPos + LettresATirees Do
          Str := Str + Chr(Sac[I] + 64);
        LettresJoueur1.Caption := Str;
        SacPos := SacPos + LettresATirees + 1;

        ScoreJoueur1.Caption := IntToStr(StrToInt(ScoreJoueur1.Caption) + resultat);
      End;
    2 :
      Begin
        CurStr := LettresJoueur2.caption;
        For I := 1 To Length(Str) Do
          Begin
            Delete(CurStr,Pos(Str[i], CurStr),1);
          End;
        Str := CurStr;

        Sac := Remelanger(Sac,SacPos);

        IF CurStr = '' Then ScoreJoueur2.Caption := IntToStr(StrToInt(ScoreJoueur2.Caption) + 50);

        LettresATirees := 6 - Length(CurStr);
        IF SacPos + LettresATirees > 99 Then
          LettresATirees := 99 - SacPos;

        For I := SacPos To SacPos + LettresATirees Do
          Str := Str + Chr(Sac[I] + 64);
        LettresJoueur2.Caption := Str;
        SacPos := SacPos + LettresATirees + 1;

        ScoreJoueur2.Caption := IntToStr(StrToInt(ScoreJoueur2.Caption) + resultat);
      End;
    3 :
      Begin
        CurStr := LettresJoueur3.caption;
        For I := 1 To Length(Str) Do
          Begin
            Delete(CurStr,Pos(Str[i], CurStr),1);
          End;
        Str := CurStr;

        Sac := Remelanger(Sac,SacPos);

        IF CurStr = '' Then ScoreJoueur3.Caption := IntToStr(StrToInt(ScoreJoueur3.Caption) + 50);

        LettresATirees := 6 - Length(CurStr);
        IF SacPos + LettresATirees > 99 Then
          LettresATirees := 99 - SacPos;

        For I := SacPos To SacPos + LettresATirees Do
          Str := Str + Chr(Sac[I] + 64);
        LettresJoueur3.Caption := Str;
        SacPos := SacPos + LettresATirees + 1;

        ScoreJoueur3.Caption := IntToStr(StrToInt(ScoreJoueur3.Caption) + resultat);
      End;
    4 :
      Begin
        CurStr := LettresJoueur4.caption;
        For I := 1 To Length(Str) Do
          Begin
            Delete(CurStr,Pos(Str[i], CurStr),1);
          End;
        Str := CurStr;

        Sac := Remelanger(Sac,SacPos);

        IF CurStr = '' Then ScoreJoueur4.Caption := IntToStr(StrToInt(ScoreJoueur4.Caption) + 50);

        LettresATirees := 6 - Length(CurStr);
        IF SacPos + LettresATirees > 99 Then
          LettresATirees := 99 - SacPos;

        For I := SacPos To SacPos + LettresATirees Do
          Str := Str + Chr(Sac[I] + 64);
        LettresJoueur4.Caption := Str;
        SacPos := SacPos + LettresATirees + 1;

        ScoreJoueur4.Caption := IntToStr(StrToInt(ScoreJoueur4.Caption) + resultat);
      End;
  End;

IF CurJoueur < NombreJoueur Then
  Inc(CurJoueur)
Else
  Begin
    CurJoueur := 1;
    Inc(Tour);
  End;

IF LettresJoueur1.Caption = '' Then
    ScoreJoueur1.Caption := IntToStr(StrToInt(ScoreJoueur1.Caption) + ValeurLettreRestantes(LettresJoueur1.Caption + lettresJoueur2.Caption + LettresJoueur3.Caption + LettresJoueur4.Caption));
IF LettresJoueur2.Caption = '' Then
    ScoreJoueur2.Caption := IntToStr(StrToInt(ScoreJoueur1.Caption) + ValeurLettreRestantes(LettresJoueur1.Caption + lettresJoueur2.Caption + LettresJoueur3.Caption + LettresJoueur4.Caption));
IF LettresJoueur3.Caption = '' Then
    ScoreJoueur3.Caption := IntToStr(StrToInt(ScoreJoueur1.Caption) + ValeurLettreRestantes(LettresJoueur1.Caption + lettresJoueur2.Caption + LettresJoueur3.Caption + LettresJoueur4.Caption));
IF LettresJoueur4.Caption = '' Then
    ScoreJoueur4.Caption := IntToStr(StrToInt(ScoreJoueur1.Caption) + ValeurLettreRestantes(LettresJoueur1.Caption + lettresJoueur2.Caption + LettresJoueur3.Caption + LettresJoueur4.Caption));


EchangerBtn;

LblTour.Caption := 'Tour ' + IntToStr(Tour);
Insert.Caption := '0, 0';
LblRestantes.caption := 'Lettres restantes : ' + IntToStr(100 - SacPos);

end;






procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);

begin
  DebutX := 16 - ((Image1.Width - X) div (Image1.Width div 15) + 1);
  DebutY := 16 - ((Image1.Height - y) div (Image1.Height div 15) + 1);
  Insert.Caption := IntToStr(DebutX) + ', ' + IntToStr(DebutY);
end;





Procedure Ouvrir;
Begin
  Form1.Image1.Picture.LoadFromFile('Tmp.bmp');
  DeleteFile('Tmp.bmp');
  SacPos := ProcedureUnit.SacPosSave;
  Afficher.Tab := ProcedureUnit.Tab;
  Afficher.Plateau := procedureUnit.Plateau;
  CurJoueur := ProcedureUnit.JoueurSave;
  Form1.LblTour.Caption := 'Tour ' + IntToStr(ProcedureUnit.TourSave);
  Form1.LblRestantes.caption := 'Lettres restantes : ' + IntToStr(100 - SacPos);
  PremLettre := ProcedureUnit.PremLettreSave;
  Case CurJoueur Of
    1 :
      Begin
        Form1.LettresJoueur1.Caption := ProcedureUnit.LettresSave;
        Form1.ScoreJoueur1.Caption := IntToStr(ProcedureUnit.ScoresSave);
      End;
    2 :
      Begin
        Form1.LettresJoueur2.Caption := ProcedureUnit.LettresSave;
        Form1.ScoreJoueur2.Caption := IntToStr(ProcedureUnit.ScoresSave);
      End;
    3 :
      Begin
        Form1.LettresJoueur3.Caption := ProcedureUnit.LettresSave;
        Form1.ScoreJoueur3.Caption := IntToStr(ProcedureUnit.ScoresSave);
      End;
    4 :
      Begin
        Form1.LettresJoueur4.Caption := ProcedureUnit.LettresSave;
        Form1.ScoreJoueur4.Caption := IntToStr(ProcedureUnit.ScoresSave);
      End;

  End;

  EchangerBtn;


End;


procedure TForm1.Annuler1Click(Sender: TObject);
begin
  Ouvrir;
end;


Procedure Echanger(LettresJoueur : TLabel);

var LettresEchangees, Lettres,LettresSave, CurLettres, CurStr, Str : string;
    I, J, LettresATirees : Byte;
    Ok : Boolean;
    Score : integer;
begin
  Repeat
    Lettres := LettresJoueur.Caption;
    CurLettres := Lettres;
    Ok := true;

    LettresEchangees := UpperCase(InputBox('Lettres à échanger','Quelles lettres voulez vous échanger?',''));
    For I := 1 To Length(LettresEchangees) Do
      Begin
        IF pos(LettresEchangees[I],CurLettres) <> 0 Then
          Delete(CurLettres,Pos(LettresEchangees[I],CurLettres),1)
        Else
          Begin
            ShowMessage('Vous ne possédez pas toute les lettres que vous avez saisie!');
            OK := False;
            Break;
          End;
      End;
    IF Length(LettresEchangees) >= 100 - SacPos Then ShowMessage('Il ne reste pas assez de lettres dans le sac pour échanger toute les lettres que vous avez saisies!');
  Until (Ok = True) And (Length(LettresEchangees) <= 100 - SacPos);

  IF LettresEchangees = '' Then
    If MessageDlg('Vous n''avez entré aucune lettres. Voulez vous passer votre tour?',mtInformation,[mbYes, mbNo],0) =
       MrNo Then
       Exit;


  LettresJoueur.Caption := CurLettres;

  J := 0;

  For I := SacPos - Length(LettresEchangees) To SacPos - 1 Do
    Begin
      Inc(J);
      Sac[I] := Ord(LettresEchangees[J]) - 64;
    End;

  SacPos := SacPos - Length(LettresEchangees);
  Sac := ReMelanger(Sac, SacPos);


    Case CurJoueur Of
    1 : Begin
          Score := StrToInt(Form1.ScoreJoueur1.Caption);
          LettresSave := Form1.LettresJoueur1.caption;
        End;
    2 : Begin
          Score := StrToInt(Form1.ScoreJoueur2.Caption);
          LettresSave := Form1.LettresJoueur2.caption;
        End;
    3 : Begin
          Score := StrToInt(Form1.ScoreJoueur3.Caption);
          LettresSave := Form1.LettresJoueur3.caption;
        End;
    4 : Begin
          Score := StrToInt(Form1.ScoreJoueur4.Caption);
          LettresSave := Form1.LettresJoueur4.caption;
       End;
 End;


Sauver(Form1.Image1, SacPos, CurJoueur, Score, Tour, LettresSave, PremLettre);

IF CurJoueur < NombreJoueur Then
  Inc(CurJoueur)
Else
  Begin
    CurJoueur := 1;
    Inc(Tour);
  End;


  CurStr := LettresJoueur.caption;
    For I := 1 To Length(Str) Do
      Begin
        Delete(CurStr,Pos(Str[i], CurStr),1);
      End;

If Length(CurStr) <> 7 Then
  Begin
    Str := CurStr;
    LettresATirees := 6 - Length(CurStr);
    IF SacPos + LettresATirees > 99 Then LettresATirees := 99 - SacPos;
    For I := SacPos To SacPos + LettresATirees Do Str := Str + Chr(Sac[I] + 64);
    LettresJoueur.Caption := Str;
    SacPos := SacPos + LettresATirees + 1;
  End;

  Form1.LblTour.Caption := 'Tour ' + IntToStr(Tour);
  Form1.Insert.Caption := '0, 0';
  Form1.LblRestantes.caption := 'Lettres restantes : ' + IntToStr(100 - SacPos);


  EchangerBtn;


end;



procedure TForm1.Joueur1EchangerClick(Sender: TObject);

begin
  Echanger(LettresJoueur1);
end;



procedure TForm1.Joueur3EchangerClick(Sender: TObject);
begin
  Echanger(LettresJoueur3);
end;

procedure TForm1.Joueur2EchangerClick(Sender: TObject);
begin
  Echanger(LettresJoueur2);
end;

procedure TForm1.Joueur4EchangerClick(Sender: TObject);
begin
  Echanger(LettresJoueur4);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  EnCour.Font.Color := ClWhite;
  EnCour.Caption := 'Calcule de la meilleur Solution en cour ...';
  application.ProcessMessages;
  Case CurJoueur Of
  1: BotJouer(LettresJoueur1.Caption);
  2: BotJouer(LettresJoueur2.Caption);
  3: BotJouer(LettresJoueur3.Caption);
  4: BotJouer(LettresJoueur4.Caption);
  End;
  EnCour.Caption := 'Calcule Fini';
end;

end.
